//
//  PaperBaseView.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/3/10.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperBaseView.h"

@interface PaperBaseView ()

@property (nonatomic, strong) UIImageView *slideImageView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *slideView;

@property (nonatomic, assign) BOOL isTop;
@property (nonatomic, assign) BOOL isBottom;

@end

@implementation PaperBaseView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupConfigure];
    }
    return self;
}

- (void)setupConfigure {
    [self addSubview:self.materialView];
    [self addSubview:self.slideImageView];
    [self addSubview:self.lineView];
    [self addSubview:self.slideView];
    
    self.isTop = NO;
    self.isBottom = NO;
    self.showMaterial = NO;
}

- (void)bindModel:(PaperBaseModel *)model { }

- (void)setupViewFrame:(BOOL)showMaterial {
    if (showMaterial == YES) {
        [self setTopType];
        self.isTop = YES;
        self.isBottom = NO;
    }
}

- (void)didActionWithData:(NSDictionary *)data { }

#pragma mark - btn action

- (void)panning:(UIPanGestureRecognizer *)panGR {
    CGPoint translation = [panGR translationInView:self];
    if (self.isTop == YES) {
        if (translation.y > 0) {
            self.isTop = NO;
        } else {
            [self setTopType];
        }
        return;
    }
    
    if (self.isBottom == YES) {
        if (translation.y < 0) {
            self.isBottom = NO;
        } else {
            [self setBottomType];
        }
        return;
    }

    
    CGFloat panTop = panGR.view.top;
    if (panTop < 120.0) {
        panGR.view.top = 120.0;
        self.isTop = YES;
    } else if (panTop > SCREEN_HEIGHT - 300.0f) {
        panGR.view.top = SCREEN_HEIGHT - 300.0f;
        self.isBottom = YES;
    } else {
        panGR.view.top += translation.y;
    }
    
    [panGR setTranslation:CGPointZero inView:self];
    
    self.slideImageView.top = panGR.view.top;
    self.lineView.top = panGR.view.bottom;
    
    self.answerView.contentInset = UIEdgeInsetsZero;
    self.answerView.top = panGR.view.bottom + 1;
    self.answerView.height = SCREEN_HEIGHT - 104 - self.slideView.bottom;
    
    self.materialView.top = 0;
    self.materialView.contentInset = UIEdgeInsetsZero;
    self.materialView.height = panGR.view.bottom;
}

- (void)setTopType {
    self.slideView.top = 120.0001f;
    self.slideImageView.top = self.slideView.top;
    self.lineView.top = self.slideView.bottom;
    
    self.answerView.top = self.slideView.bottom + 1;
    self.answerView.contentInset = UIEdgeInsetsZero;
    self.answerView.height = SCREEN_HEIGHT - 104 - self.slideView.bottom;
    
    self.materialView.top = 0;
    self.materialView.contentInset = UIEdgeInsetsZero;
    self.materialView.height = self.slideView.bottom;
}

- (void)setBottomType {
    self.slideView.top = SCREEN_HEIGHT - 300.0f;
    self.slideImageView.top = self.slideView.top;
    self.lineView.top = self.slideView.bottom;
    self.answerView.top = self.slideView.bottom + 1;
    
    self.answerView.contentInset = UIEdgeInsetsZero;
    self.answerView.height = SCREEN_HEIGHT - 104 - self.slideView.bottom;
    self.materialView.contentInset = UIEdgeInsetsZero;
    self.materialView.height = self.slideView.bottom;
}

- (void)setShowMaterial:(BOOL)showMaterial {
    _showMaterial = showMaterial;
//    self.materialView.hidden = !showMaterial;
//    self.slideImageView.hidden = !showMaterial;
//    self.lineView.hidden = !showMaterial;
//    self.slideView.hidden = !showMaterial;
}

#pragma mark - getter
- (PaperMaterialScrollView *)materialView {
    if (_materialView == nil) {
        _materialView = [[PaperMaterialScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 104)];
    }
    return _materialView;
}

- (UIImageView *)slideImageView {
    if (_slideImageView == nil) {
        _slideImageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH  * 13 / 32, 120, SCREEN_WIDTH * 6 / 32, SCREEN_WIDTH / 17)];
        _slideImageView.image = [UIImage imageNamed:@"PaperSlideBtnIcon"];
    }
    return _slideImageView;
}

- (UIView *)lineView {
    if (_lineView == nil) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 120+SCREEN_WIDTH / 17, SCREEN_WIDTH, 1)];
        _lineView.backgroundColor = RGB(234, 234, 234);
    }
    return _lineView;
}

- (UIView *)slideView {
    if (_slideView == nil) {
        _slideView = [[UIView alloc] initWithFrame:CGRectMake(0, 120, SCREEN_WIDTH, SCREEN_WIDTH / 17)];
        // 添加平移手势
        UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panning:)];
        [_slideView addGestureRecognizer:panGR];
    }
    return _slideView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
